package com.android.fragmentdemo.defineviews
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.android.fragmentdemo.R

class FloatItemDecoration(context: Context) : RecyclerView.ItemDecoration() {

    private val mContext = context

    private val mTitleHeight0 = mContext.resources.getDimension(R.dimen.dp_30).toInt()
    private val mTitleHeight1 = mContext.resources.getDimension(R.dimen.dp_1).toInt()

    private val mTextTitleSize = mContext.resources.getDimension(R.dimen.sp_16)

    private val mLeftSpace = mContext.resources.getDimension(R.dimen.dp_5)

    private  val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            color = mContext.resources.getColor(R.color.color_00BFA5)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            color = mContext.resources.getColor(R.color.color_00E5FF)
        }
    }

    private val mTextPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            textSize = mTextTitleSize
            color = mContext.resources.getColor(R.color.color_BF360C)
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val position = parent.getChildAdapterPosition(view)
        outRect.top = if (position % 4 == 0) mTitleHeight0 else mTitleHeight1
        //val itemCount = parent.adapter?.itemCount ?: 0
        /*if (position % 4 == 0) {
            outRect.top = mTitleHeight0
        } else {
            if (position != itemCount - 1) {
                outRect.top = mTitleHeight1
            } else {
                outRect.top = mTitleHeight1
            }
        }*/
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(canvas, parent, state)
        val itemCount = parent.adapter?.itemCount ?: 0
        for (i in 0.until(itemCount)) {
            val child = parent.getChildAt(i) ?: continue
            val rect = Rect()

            val position = parent.getChildAdapterPosition(child)
            if (isTitleItem(position)) {
                rect.top = child.top - mTitleHeight0
                rect.bottom = child.top
            } else {
                rect.top = child.top - mTitleHeight1
                rect.bottom = child.top
            }

            rect.left = parent.paddingLeft
            rect.right = parent.width - parent.paddingRight

            if (isTitleItem(position)) {
                canvas.drawRect(rect, mPaint0)
                val text = getTitleStr(position)
                val textRect = Rect()
                mTextPaint.getTextBounds(text, 0, text.length, textRect)
                canvas.drawText(
                    text,
                    mLeftSpace,
                    rect.top + mTitleHeight0 / 2.0F + (textRect.bottom - textRect.top) / 2.0F - textRect.bottom,
                    mTextPaint
                )
            } else {
                canvas.drawRect(rect, mPaint1)
            }
        }
    }

    override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(canvas, parent, state)

        val firstChild = parent.getChildAt(0)
        var nextChild: View? = null
        if (parent.childCount > 1) {
            nextChild = parent.getChildAt(1)
        }

        if (firstChild != null) {
            val rect = Rect()

            val position = parent.getChildAdapterPosition(firstChild)
            if (nextChild != null
                && nextChild.bottom - mTitleHeight0 < parent.paddingTop
                && isTitleItem(parent.getChildAdapterPosition(nextChild))
                && !isSameTitle(parent.getChildAdapterPosition(nextChild), position)
            ) {
                rect.top = firstChild.bottom - mTitleHeight0
                rect.bottom = firstChild.bottom
            } else {
                rect.top = parent.paddingTop
                rect.bottom = rect.top + mTitleHeight0
            }

            rect.left = parent.paddingLeft
            rect.right = parent.width - parent.paddingRight
            canvas.drawRect(rect, mPaint0)
            val text = getTitleStr(position)
            val textRect = Rect()
            mTextPaint.getTextBounds(text, 0, text.length, textRect)
            canvas.drawText(
                text,
                mLeftSpace,
                rect.top + mTitleHeight0 / 2.0F + (textRect.bottom - textRect.top) / 2.0F - textRect.bottom,
                mTextPaint
            )
        }
    }

    private fun isTitleItem(position: Int) = position % 4 == 0

    private fun getTitleStr(position: Int) : String = "标题:${position / 4}"

    private fun isSameTitle(position0: Int, position1: Int) = position0 / 4 == position1 / 4
}